//
//  ZZCTransferController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCTransferController.h"
#import "ZZCTransferDetailsController.h"
#import "ZZCLookPayPasswordController.h"
#import "ZZCOwnPropertyController.h"
@interface ZZCTransferController ()<UITextFieldDelegate>
@property (weak,   nonatomic) IBOutlet UITextField *userPhoneTextField;
@property (weak,   nonatomic) IBOutlet UITextField *buyCountTextField;
@property (strong, nonatomic) GRBkeyTextField * safeTextField;
@property (strong, nonatomic) GRBkeyTextField * safeInputTextField;
@property (copy,   nonatomic) NSString *payPwd;

@property(nonatomic,strong) GRBsafeKeyBoard * board;
@end

@implementation ZZCTransferController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"转账";
    IQKeyboardManager *iq = [IQKeyboardManager sharedManager];
    iq.enableAutoToolbar = NO;
    // Do any additional setup after loading the view.
}
- (IBAction)confirmClick {
    if (self.userPhoneTextField.text == nil) {
        [SVProgressHUD showInfoWithStatus:@"请输入转账用户"];
        return;
    }
    if (self.buyCountTextField.text == nil) {
        [SVProgressHUD showInfoWithStatus:@"请输入转账数量"];
        return;
    }

    self.board = [GRBsafeKeyBoard GRB_showSafeInputKeyBoard];
    
    __weak typeof(self)weakSelf = self;
    self.board.GRBsafeKeyFinish=^(NSString * passWord){
        if (passWord.length==6) {
            
            [weakSelf.board GRB_endKeyBoard];
            ZZCLog(@"你输入的密码为---%@",passWord);
            weakSelf.payPwd = passWord;
            [weakSelf finishAction];

            //在这里判断密码是否正确
            if (YES) {
                ZZCLog(@"你输入的密码正确");
                
            }else {
                ZZCLog(@"你输入的密码错误");
            }
        }
        
    };
    self.board.GRBsafeKeyClose=^{
        
        [weakSelf.board GRB_endKeyBoard];
    };
    self.board.GRBsafeKeyForgetPassWord=^{
        ZZCLog(@"点击忘记密码了");
        ZZCLookPayPasswordController *VC = [ZZCLookPayPasswordController new];
        [weakSelf.navigationController pushViewController:VC animated:YES];
    };
    
}
- (void)finishAction {

    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{
                           @"token":token,
                           @"money":self.buyCountTextField.text ,
                           @"phone":self.userPhoneTextField.text ,
                           @"pay_password":_payPwd
                           };
    NSLog(@"%@",dict);
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
                            @"token":token,
                            @"money":self.buyCountTextField.text ,
                            @"phone":self.userPhoneTextField.text ,
                            @"pay_password":_payPwd
                            };
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_TURNPERSONAL parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
            
        }else {
            ZZCLog(@"-*************%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if ([responseObject[@"code"] isEqual:@(200)]) {
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ZZCOwnPropertyController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                    }
                }
            }
//            _reg.UbiCountLabel.text = [NSString stringWithFormat:@"%.2fU币",[responseObject[@"data"][@"money"] floatValue]];
            //            NSLog(@"%@/%@",ZZCURL_API_USERASSETS,token);
            //            self.count = responseObject[@"data"][@"count"];
            //            self.dueModel = [ZZCDue mj_objectWithKeyValues:responseObject[@"data"][@"list"]];
            //            [self.dueTableView reloadData];
        }
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    if (textField == self.userPhoneTextField) {
        //用来做长度限制；
        if (range.location>=11) {
            return NO;
        }
    }
    return YES;
    
}



@end
